﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Net.Mail;
using Jerrysjunglehuts.Models;
using System.ComponentModel;

namespace Jerrysjunglehuts.Controllers
{
    public class TariffController : Controller
    {
        // GET: Tariff
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Package()
        {
            return View();
        }

        public ActionResult ResortYourselves()
        {
            return View();
        }

        public ActionResult LongStay()
        {
            return View();
        }

        public ActionResult PayAsYouLike()
        {
            return View();
        }
        
        public ActionResult KandbariHills() {
            return View();
        }

        public ActionResult KandbariHillsGallery()
        {
            return View();
        }

        public ActionResult BookingRoom([DefaultValue("")]string r, [DefaultValue("")]string ci, [DefaultValue("")]string co, [DefaultValue(0)]int a, [DefaultValue(0)]int c)
        {
            try
            {
                BookingViewModel ob = new BookingViewModel();
                if (Session["BookingData"] != null)
                {
                    ob = (BookingViewModel)Session["BookingData"];
                }              
                return View(ob);
            }
            catch (Exception ex)
            {
            }
            return View("");
        }

        [HttpPost]
        public JsonResult SendMail(BookingFullViewModel ob)
        {
            try
            {
                SendMailer(ob);
                SendMailerToUser(ob);
                return Json("success");

            }
            catch (Exception ex)
            {
            }
            return Json("fail");
        }

        public Boolean SendMailerToUser(BookingFullViewModel ob)
        {
            try
            {
                if (!ob.s_checkInDate.Equals("") && !ob.s_checkoutDate.Equals("") && !ob.emailID.Trim().Equals(""))
                {
                    var splits = ob.s_checkInDate.Split('/');
                    var checkInDate = new DateTime(Convert.ToInt32(splits[2]), Convert.ToInt32(splits[1]), Convert.ToInt32(splits[0]));
                    var splitsCheckOut = ob.s_checkoutDate.Split('/');
                    var checkoutDate = new DateTime(Convert.ToInt32(splitsCheckOut[2]), Convert.ToInt32(splitsCheckOut[1]), Convert.ToInt32(splitsCheckOut[0]));
                    ob.checkInDate = checkInDate;
                    ob.checkoutDate = checkoutDate;
                    var message = new MailMessage();
                    MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
                    MailAddress receiver = new MailAddress(ob.emailID);
                    SmtpClient smtp = new SmtpClient()
                    {
                        Host = ConfigurationManager.AppSettings["smtpServer"],
                        Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
                        EnableSsl = true,
                        Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"])
                    };
                    message.From = Sender;
                    message.To.Add(receiver);                 
                    message.Subject = "Jerry’s Jungle Huts Query [" + ob.fullName + " ]";
                    var bodyMessage = "<p>Dear Guest,</p><p>We thank you very much for showing interest in Jerry’s Jungle Huts! It would be our absolute pleasure to be of service to you.</p><p>We are in receipt of your mail and have noted down your requirement. Kindly allow us two working days to respond to your booking/query.</p>";                    
                    bodyMessage += "<br/><br/><br/><p>With warm regards;</p><p>Shakti Sharan</p><p>COO-Jerry’s Jungle Huts</p>";
                    message.Body = bodyMessage;
                    message.IsBodyHtml = true;
                    smtp.Send(message);
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public Boolean SendMailer(BookingFullViewModel ob)
        {
            try
            {
                if (!ob.s_checkInDate.Equals("") && !ob.s_checkoutDate.Equals("") && !ob.emailID.Trim().Equals(""))
                {
                    var splits = ob.s_checkInDate.Split('/');
                    var checkInDate = new DateTime(Convert.ToInt32(splits[2]), Convert.ToInt32(splits[1]), Convert.ToInt32(splits[0]));
                    var splitsCheckOut = ob.s_checkoutDate.Split('/');
                    var checkoutDate = new DateTime(Convert.ToInt32(splitsCheckOut[2]), Convert.ToInt32(splitsCheckOut[1]), Convert.ToInt32(splitsCheckOut[0]));
                    ob.checkInDate = checkInDate;
                    ob.checkoutDate = checkoutDate;
                    var message = new MailMessage();
                    MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
                    MailAddress receiver = new MailAddress(ob.emailID);
                    SmtpClient smtp = new SmtpClient()
                    {
                        Host = ConfigurationManager.AppSettings["smtpServer"],
                        Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
                        EnableSsl = true,
                        Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"])
                    };
                    message.From = Sender;
                    message.To.Add("shaktisharan@yahoo.com");
                    message.CC.Add(new MailAddress("info@jerrysjunglehuts.com"));
                    message.Bcc.Add(new MailAddress("raviprrai@gmail.com"));
                    message.Bcc.Add(new MailAddress("info.goldenbrush@gmail.com"));
                    message.Subject = "Jerry’s Jungle Huts Query [" + ob.fullName + " ]";
                    var bodyMessage = "<p>Dear Owner,</p><p>One Booking request has been made through portal and details are as follow:</p>";
                    bodyMessage += "<br/><table border='1px' style='border-style: solid;color:grey'><tr><td>Name</td><td>" + ob.fullName + "</td></tr>";
                    bodyMessage += "<tr><td>ContactNo</td><td>" + ob.telMobile + "</td></tr>";
                    bodyMessage += "<tr><td>Email</td><td>" + ob.emailID + "</td></tr>";
                    bodyMessage += " <tr><td>CheckInDate</td><td>" + Convert.ToDateTime(checkInDate).ToLongDateString() + "</td></tr>";
                    bodyMessage += " <tr><td>CheckOutDate</td><td>" + Convert.ToDateTime(checkoutDate).ToLongDateString() + "</td></tr>";
                    bodyMessage += " <tr><td>MealPreference</td><td>" + ob.MealPreference + "</td></tr>";
                    bodyMessage += " <tr><td>RoomType</td><td>" + ob.RoomType + "</td></tr>";
                    bodyMessage += " <tr><td>Tariff</td><td>" + ob.tariff + "</td></tr>";
                    bodyMessage += " <tr><td>No.of Adults</td><td>" + ob.noOfAdults + "</td></tr>";
                    bodyMessage += " <tr><td>No.of Children</td><td>" + ob.noOfChildren + "</td></tr>";
                    bodyMessage += " <tr><td>Rooms Required</td><td>" + ob.requirementRooms + "</td></tr>";
                    bodyMessage += " <tr><td>Coming From</td><td>" + ob.comingFrom + "</td></tr>";
                    bodyMessage += " <tr><td>Special Request</td><td>" + ob.specialpRequest + "</td></tr></table>";
                    bodyMessage += "<br/><br/><br/><p>With warm regards;</p><p>Shakti Sharan</p><p>COO-Jerry’s Jungle Huts</p>";
                    message.Body = bodyMessage;
                    message.IsBodyHtml = true;
                    smtp.Send(message);
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}